//获取传递过来的id
String.prototype.parseUrl = function() {
    if (this.indexOf("?") === -1) {
        return {};
    }
    let result = {}
    this.slice(this.indexOf("?") + 1).split("&").forEach(item => {
        let arr = item.split("=");
        result[arr[0]] = arr[1]
    })
    return result;
}
let id = location.href.parseUrl().id;
// console.log(id)
//============发送请求==========================
$.ajax({
        method: "get",
        url: "http://192.168.50.142:81/countgoods",
        data: {
            id: id
        },
        success: function(data) {
            let dataList = JSON.parse(data);
            let template = $("#biggoods");
            dataList.forEach((item) => {
                let newitem = template.clone(true);
                newitem.attr("id", "");
                newitem.find(".goodsname").html(item.text);
                newitem.find(".leftimg").attr("src", item.goodimg);
                newitem.find(".rightimg").attr("src", item.bigbigimg);
                newitem.find(".biggoods-three h2").html(item.text);
                newitem.find(".lione").html(item.xinghao);
                newitem.find(".litwo").html(item.bianhao);
                newitem.find(".lithree").html(item.pinpai);
                newitem.find(".lifore").html(item.xiaoliang);
                newitem.find(".oldmoney").html(item.newmoney);
                newitem.find(".newmoney").html(item.oldmoney);
                newitem.find(".lifive").attr("ids", item.nummonwy)
                    //newitem.appendTo($("#goodsNumbs-boder"))
                $("#biggoods").before(newitem)
            })
        }



    })
    //======点击购买后===============================
$("#goumai").click(function() {
        let shuju = $(".newmoney").html();
        // 价格
        let a = shuju.split("￥")[1];
        //数量
        let number = $(".numtwo").html();
        if (Number(number) <= 0) {
            alert("请填写购买商品数量");
            return;
        }
        //总价
        let count = Number(a) * Number(number);
        // console.log(count);
        //共计多少商品

        localStorage.setItem("onea", number);
        //共计多少钱
        localStorage.setItem("oneb", count);
        //保存到历史记录里
        //姓名
        let gindname = sessionStorage.getItem("stuname");
        if (gindname == null) {
            alert("请登录");
            location.href = "http://192.168.50.142/MIDO/denglu.html";
            return;
        };
        //获取到图片路径用id代替
        let limg = $(".leftimg").attr("src");
        console.log(limg);
        //获取商品名称
        let newgoodname = $(".biggoods-three  h2").html();
        console.log(newgoodname);
        //价格
        let newnewmoney = $(".lifive").attr("ids");
        //获取时间
        let date = new Date();
        let newdate = date.toLocaleString();
        $.ajax({
                method: "get",
                url: "http://192.168.50.142:81/newonename",
                data: {
                    limg: limg,
                    newgoodname: newgoodname,
                    newnewmoney: newnewmoney,
                    gindname: gindname,
                    newdate: newdate
                },
                success: function(data) {
                    if (data === "success") {
                        console.log("success");
                    } else {
                        console.log("errer");
                    }
                }
            })
            //直接跳转到填写收件人页面
        location.href = "http://192.168.50.142/MIDO/dingdan.html";
    })
    //======点击加入购物车===============================
$("#gouwuche").click(function() {
    localStorage.setItem("id", id);
    //姓名
    let gindname = sessionStorage.getItem("stuname");
    if (gindname == null) {
        alert("请登录");
        location.href = "http://192.168.50.142/MIDO/denglu.html";
        return;
    };
    //获取到图片路径用id代替
    let limg = $(".leftimg").attr("src");
    console.log(limg);
    //获取商品名称
    let newgoodname = $(".biggoods-three  h2").html();
    console.log(newgoodname);
    //价格
    let newnewmoney = $(".lifive").attr("ids");
    //获取时间
    let date = new Date();
    let newdate = date.toLocaleString();
    $.ajax({
        method: "get",
        url: "http://192.168.50.142:81/newonename",
        data: {
            limg: limg,
            newgoodname: newgoodname,
            newnewmoney: newnewmoney,
            gindname: gindname,
            newdate: newdate
        },
        success: function(data) {
            if (data === "success") {
                console.log("success");
            } else {
                console.log("errer");
            }
        }
    })

    //=========添加到购物车表===============
    // 价格
    let shuju = $(".newmoney").html();
    shuju = shuju.split("￥")[1];
    alert(limg);
    alert(newgoodname);
    alert(shuju);
    $.ajax({
            method: "get",
            url: "http://192.168.50.142:81/newgouwuche",
            data: {
                limg: limg,
                newgoodname: newgoodname,
                shuju: shuju
            },
            success: function(data) {
                if (data === "success") {
                    alert("添加成功");
                } else {
                    console.log("添加失败");
                }
            }
        })
        //location.href="http://192.168.50.142/MIDO/gouwuche.html";
})

//===========增加和减少==========
$(".numthree").click(function() {
    let num = $("#numlist .numtwo").html();
    $(".numtwo").html(Number(num) + 1);;

})
$(".numone").click(function() {
        let num = $("#numlist .numtwo").html();
        num = Number(num) - 1;
        if (num < 0) {
            num = 0;
        }
        $(".numtwo").html(num);

    })
    //==========================放大镜==========================

$("#biggoods-leftimg").mouseover(function() {
    $(".smallbox").css("display", "block");
    $("#biggoods-leftimg").css("border", "1px solid  #999999")
    $("#biggoods-rightimg").css("display", "block");
})
$("#biggoods-leftimg").mouseout(function() {
    $(".smallbox").css("display", "none");
    $("#biggoods-leftimg").css("border", "0px solid  #999999")
    $("#biggoods-rightimg").css("display", "none");
})
$("#biggoods-leftimg").mousemove(function(evt) {
        let e = evt || event;
        //放大镜移动
        let boxLeft = e.pageX - $("#biggoods-leftimg").offset().left - $(".smallbox").width() / 2;
        let boxtop = e.pageY - $("#biggoods-leftimg").offset().top - $(".smallbox").height() / 2;
        if (boxLeft < 0) {
            boxLeft = 0;
        }
        if (boxtop < 0) {
            boxtop = 0;
        }
        let boxright = $("#biggoods-leftimg").width() - $(".smallbox").width();
        let boxbutton = $("#biggoods-leftimg").height() - $(".smallbox").height();
        if (boxLeft > boxright) {
            boxLeft = boxright;
        }
        if (boxtop > boxbutton) {
            boxtop = boxbutton;
        }
        $(".smallbox").css("left", boxLeft + "px");
        $(".smallbox").css("top", boxtop + "px");
        //大图的移动
        let bigImgLeft = $(".rightimg").width() * boxLeft / $("#biggoods-leftimg").width();
        let bigImgTop = $(".rightimg").height() * boxtop / $("#biggoods-leftimg").height();
        $(".rightimg").css("left", -bigImgLeft + "px");
        $(".rightimg").css("top", -bigImgTop + "px");
    })
    //判断用户是否的登录===========================================================
    //跳转个人
$(".nolookone").click(function() {
        //获取到页面保存的值
        let phooo = sessionStorage.getItem("stuname");
        console.log(phooo);
        if (phooo == null) {
            alert("请登录");
            location.href = "http://192.168.50.142/MIDO/denglu.html";
            return;
        };
        //判断是否穿在
        location.href = "http://192.168.50.142/MIDO/gerenxinxi.html";
    })
    //跳转购物车
$(".nolooktwo").click(function() {
        //获取到页面保存的值
        let phooo = sessionStorage.getItem("stuname");
        console.log(phooo);
        if (phooo == null) {
            alert("请登录");
            location.href = "http://192.168.50.142/MIDO/denglu.html";
            return;
        };
        //判断是否穿在
        location.href = "http://192.168.50.142/MIDO/gouwuche.html";
    })
    //========================
    //判断是否登录
    //获取到页面保存的值
function qqj() {
    let phooo = sessionStorage.getItem("stuname");
    if (phooo !== null) {
        $
        let yesname = $(".yesdenglu").html();
        yesname = "已登录";
        $(".yesdenglu").html(yesname);
        console.log(yesname);
    } else {
        let yesname = $(".yesdenglu").html();
        yesname = "请登录";
        $(".yesdenglu").html(yesname);
        console.log(yesname);
    }
}
qqj();